package project.vip.services.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kernel.util.StringUtils;
import kernel.web.Page;
import kernel.web.PagedQueryDao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import project.vip.po.Inv;
import project.vip.services.AdminInvService;

public class AdminInvServiceImpl extends HibernateDaoSupport implements AdminInvService {

    private PagedQueryDao pagedQueryDao;

    @Override
    public List<Inv> getInvList(String toPhone,boolean isDeposit) {
    	if(isDeposit){
            StringBuffer queryString = new StringBuffer(" FROM Inv WHERE toPhone = ? AND isDeposit = true ORDER BY createTime desc");
            return this.getHibernateTemplate().find(queryString.toString(),new Object[]{toPhone});
    	}else{
            StringBuffer queryString = new StringBuffer(" FROM Inv WHERE toPhone = ? AND isDeposit = false ORDER BY createTime desc");
            return this.getHibernateTemplate().find(queryString.toString(),new Object[]{toPhone});
    	}
    }
    
    @Override
    public List<Inv> getInvList4openId(String toOpenId) {
        StringBuffer queryString = new StringBuffer(" FROM Inv WHERE toOpenId = ? ORDER BY createTime desc");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{toOpenId});
    }
    
    @Override
    public List<Inv> getInvList(String toPhone) {
        StringBuffer queryString = new StringBuffer(" FROM Inv WHERE toPhone = ? ORDER BY createTime desc");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{toPhone});
    }
    @Override
    public List<Inv> getInvList(String fromOpenId,String fromPhone) {
        StringBuffer queryString = new StringBuffer(" FROM Inv WHERE fromOpenId = ? AND fromPhone = ? ORDER BY createTime desc");
        return this.getHibernateTemplate().find(queryString.toString(),new Object[]{fromOpenId,fromPhone});
    }
    
    @Override
    public Page pagedQuery(int pageNo, int pageSize, String fromPhone,String fromOpenId) {
        StringBuffer queryString = new StringBuffer(" FROM Inv WHERE 1=1 ");

        Map<String, Object> parameters = new HashMap<String, Object>();
        
        if (!StringUtils.isNullOrEmpty(fromOpenId)) {
            queryString.append(" and fromOpenId  = :fromOpenId");
            parameters.put("fromOpenId", fromOpenId);
        }
        
        if (!StringUtils.isNullOrEmpty(fromPhone)) {
            queryString.append(" and fromPhone  = :fromPhone");
            parameters.put("fromPhone", fromPhone);
        }
        queryString.append(" order by createTime desc ");
        Page page = pagedQueryDao.pagedQuery(pageNo, pageSize, queryString.toString(), parameters);
        return page;

    }

    public void setPagedQueryDao(PagedQueryDao pagedQueryDao) {
        this.pagedQueryDao = pagedQueryDao;
    }
    
    @Override
    public void save(Inv entity) {
        entity.setCreateTime(new Date());
        this.getHibernateTemplate().save(entity);

    }

    @Override
    public void update(Inv entity) {
        this.getHibernateTemplate().update(entity);

    }

}
